package com.dl.helloworld.ViewModel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.dl.helloworld.Model.DownloadProgress;

public class DownLoadViewModel extends ViewModel {

    private MutableLiveData<DownloadProgress> progressLiveData;

    public MutableLiveData<DownloadProgress> getProgressLiveData() {
        if (progressLiveData == null){
            progressLiveData = new MutableLiveData<>();
        }
        return progressLiveData;
    }

    //模拟下载
    public void startDownload(){
        new Thread(()->{
           DownloadProgress progress = new DownloadProgress(0);
            for (int i = 0; i < 101; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                progress.setProgress(i);
                progressLiveData.postValue(progress);
            }
        }).start();
    }
}
